package com.tlgen.service;

import com.tlgen.mapper.RegistryMapper;
import com.tlgen.model.Registry;
import com.tlgen.model.RespPageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: tlgen
 * @Discription:
 */
@Service
public class RegistryService {

    public static final Logger LOGGER = LoggerFactory.getLogger(RecordService.class);

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    RegistryMapper registryMapper;

    public RespPageBean getRegistryByPage(Integer page, Integer size, Registry registry) {
        if (page != null && size != null) {
            page = (page-1) * size;
        }
        List<Registry> data = registryMapper.getRegistryByPage(page, size, registry);
        Long total = registryMapper.getTotal(registry);
        RespPageBean bean = new RespPageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    /*
    *   <insert id="insertSelective" parameterType="com.tlgen.model.Registry" useGeneratedKeys="true" keyProperty="id">
    * */
    public Integer addRegistry(Registry registry) {
        registry.setCreateDate(new Date());
        int result = registryMapper.insertSelective(registry);
        if (result == 1) {
            Registry res = registryMapper.getRegistryById(registry.getId());
            LOGGER.info(res.toString());
            rabbitTemplate.convertAndSend("tlgen.mail.registry", res);
        }
        return result;
    }

    public Integer deleteRegistryById(Integer id) {
        return registryMapper.deleteByPrimaryKey(id);
    }

    public Integer updateRegistry(Registry registry) {
        return registryMapper.updateByPrimaryKeySelective(registry);
    }

}
